#include "StdAfx.h"
#include "PathAddpter.h"
CPathAddpter g_oNewPath;

CPathAddpter::CPathAddpter(void)
{
	mstrPath = _T("");
}

CPathAddpter::~CPathAddpter(void)
{
}

BOOL CPathAddpter::InitPath(CString strPath)
{
	BOOL ret = FALSE;
	HANDLE hFile = NULL;
	sPos pos;
	DWORD dwCount1 = 0, 
		  dwCount2 = 0,
		  dwCount3 = 0,
		  dwCount4 = 0,dw;

	mPosListP.clear();
	mPosListG.clear();
	mPosListR1.clear();
	mPosListR2.clear();
	mPosListF.clear();

	mstrPath = strPath;
	hFile = ::CreateFile(strPath,  GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, NULL, NULL); 
	if (hFile==INVALID_HANDLE_VALUE ) return FALSE;
	::SetFilePointer(hFile, 0, 0, FILE_BEGIN);
	if (!ReadFile(hFile, &dwCount1, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 4, 0, FILE_BEGIN);
	if (!ReadFile(hFile, &dwCount2, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 8, 0, FILE_BEGIN);
	if (!ReadFile(hFile, &dwCount3, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 12, 0, FILE_BEGIN);
	if (!ReadFile(hFile, &dwCount4, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 16, 0, FILE_BEGIN);

	for (DWORD i=0; i<dwCount1; i++)
	{
		ReadFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);

		if (dw == sizeof(sPos))
		{
			mPosListP.push_back(pos);
		}
	}

	for (DWORD i=0; i<dwCount2; i++)
	{
		ReadFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);

		if (dw == sizeof(sPos))
		{
			mPosListG.push_back(pos);
			if (pos.h>0.0)
				mPosListF.push_back(pos);
		}
	}

	for (DWORD i=0; i<dwCount3; i++)
	{
		ReadFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);

		if (dw == sizeof(sPos))
		{
			mPosListR1.push_back(pos);
		}
	}

	for (DWORD i=0; i<dwCount4; i++)
	{
		ReadFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);

		if (dw == sizeof(sPos))
		{
			mPosListR2.push_back(pos);
		}
	}
	ret = TRUE;
	CloseHandle(hFile);
	return ret;
}

BOOL CPathAddpter::WritePath()
{
	BOOL ret = FALSE;
	HANDLE hFile = NULL;
	DWORD dwCount1 = mPosListP.size(), 
		  dwCount2 = mPosListG.size(),
		  dwCount3 = mPosListR1.size(),
		  dwCount4 = mPosListR2.size(),dw;
	sPos pos;

	if (mstrPath == _T("")) return 0;
	hFile = ::CreateFile(mstrPath,  GENERIC_ALL, FILE_SHARE_READ , NULL, CREATE_ALWAYS, NULL, NULL); 
	if (hFile==INVALID_HANDLE_VALUE ) return FALSE;
	
	::SetFilePointer(hFile, 0, 0, FILE_BEGIN);
	if (!::WriteFile(hFile, &dwCount1, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 4, 0, FILE_BEGIN);
	if (!::WriteFile(hFile, &dwCount2, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 8, 0, FILE_BEGIN);
	if (!::WriteFile(hFile, &dwCount3, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 12, 0, FILE_BEGIN);
	if (!::WriteFile(hFile, &dwCount4, sizeof(DWORD), &dw, NULL))
	{
		CloseHandle(hFile);
		return FALSE;
	}
	::SetFilePointer(hFile, 16, 0, FILE_BEGIN);

	for (DWORD i=0; i<dwCount1; i++)
	{
		pos = mPosListP[i];
		WriteFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);
	}
	for (DWORD i=0; i<dwCount2; i++)
	{
		pos = mPosListG[i];
		WriteFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);
	}
	for (DWORD i=0; i<dwCount3; i++)
	{
		pos = mPosListR1[i];
		WriteFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);
	}
	for (DWORD i=0; i<dwCount4; i++)
	{
		pos = mPosListR2[i];
		WriteFile(hFile, &pos, sizeof(sPos), &dw, NULL);
		::SetFilePointer(hFile, sizeof(sPos), 0, FILE_CURRENT);
	}

	ret = TRUE;
	CloseHandle(hFile);
	return ret;
}


void CPathAddpter::GetPathList(int itype, std::vector<sPos>& Vlist)
{
	switch (itype)
	{
	    case 1:
			Vlist = mPosListP;
			break;
		case 2:
			Vlist = mPosListG;
			break;
	    case 3:
			Vlist = mPosListR1;
			break;
		case 4:
			Vlist = mPosListR2;
			break;
		case 5:
			Vlist = mPosListF;

	}
}

void CPathAddpter::AddListToFile(std::vector<sPos>& Vlist, int itype)
{
	switch (itype)
	{
	    case 1:
			mPosListP = Vlist;
			break;
		case 2:
			mPosListG = Vlist;
			break;
	    case 3:
			mPosListR1 = Vlist;
			break;
		case 4:
			mPosListR2 = Vlist;
			break;
	}
}

void CPathAddpter::AddPosToList(sPos pos, int itype)
{
	switch (itype)
	{
	    case 1:
			mPosListP.push_back(pos);
			break;
		case 2:
			mPosListG.push_back(pos);;
			break;
	    case 3:
			mPosListR1.push_back(pos);;
			break;
		case 4:
			mPosListR2.push_back(pos);;
			break;
	}	
}

void CPathAddpter::Clear()
{
	mPosListP.clear();
	mPosListG.clear();
	mPosListR1.clear();
	mPosListR2.clear();
	mPosListF.clear();
}